﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using Besture.Common;
using Besture.Common.Helper;


namespace Besture.UI.Admin.Control
{
    public partial class UploadPhoto : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";
            Response.Charset = "utf-8";
            var httpPostedFile = Request.Files["ProductPhotoFile"];
            string yearMonthPath = GetYearMonthPath();
            //上传图片路径
            string normalFolder = Request["FolderPath"] + yearMonthPath;
            //检测上传图片路径
            string absoluteNormalFolderPath = DetectionDirectory(normalFolder);
            if (httpPostedFile == null)
            {
                /*失败发送0*/
                Response.Write("0");
                Response.End();
            }
            //图片名称
            string timestamp = string.Empty;
            string photoFileName = GetPhotoFileName(httpPostedFile, ref timestamp);
            //图片路径（用于发至前台）
            string photoFilePath = yearMonthPath +"/" + photoFileName;
            //正常图片路径 
            string normalPhotoFilePath = absoluteNormalFolderPath + photoFileName;
            httpPostedFile.SaveAs(normalPhotoFilePath);

            #region 生成缩略图片

            //是否需要缩略图片
            bool NeedThumbnail = Request["NeedThumbnail"].ToLower().Equals("true");
            if (NeedThumbnail)
            {
                //检测缩略图上传图片路径 
                string thumbnailFolderPath = Request["ThumbnailFolderPath"];
                //缩略图宽度
                double thumbnailWidth = Convert.ToInt32(Request["ThumbnailWidth"]);
                //缩略图高度
                double thumbnailHeight = Convert.ToInt32(Request["ThumbnailHeight"]);
                //检测上传缩略图片路径
                string absoluteThumbnailFolderPath = DetectionDirectory(thumbnailFolderPath + GetYearMonthPath());
                //缩略图图片路径
                string thumbnailPhotoFilePath = absoluteThumbnailFolderPath + photoFileName;
                DetectionDirectory(thumbnailFolderPath + "/" + GetYearMonthPath());
                //生成缩略图片
                ImageHelper.CreatePic(normalPhotoFilePath, thumbnailPhotoFilePath, thumbnailWidth, thumbnailHeight);
                //图片地址|图片说明|图片编号
                Response.Write(string.Format(@"{0}|{1}|{2}", photoFilePath, "", timestamp));
                Response.End();

            }
            //图片地址|图片说明|
            Response.Write(string.Format(@"{0}|{1}|{2}", photoFilePath, "", timestamp));
            Response.End();

            #endregion

            
        }
        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <returns></returns>
        public string GetFolderPath(string _folderPath)
        {
            return _folderPath + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        }

        /// <summary>
        /// 获取年月文件夹
        /// </summary>
        
        /// <returns></returns>
        public string GetYearMonthPath()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 检测目录，如果无则创建
        /// </summary>
        public string DetectionDirectory(string path)
        {
            string absolutePath = Server.MapPath(path);
            if (!System.IO.Directory.Exists(absolutePath))
            {
                System.IO.Directory.CreateDirectory(absolutePath);
            }
            return absolutePath + "\\";
        }

        /// <summary>
        /// 获取图片名称
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <returns></returns>
        public string GetPhotoFileName(HttpPostedFile httpPostedFile,ref string timestamp)
        {
            timestamp = DateTimeHelper.GetTimestamp();
            var strEx = (StringHelper.GetExName(httpPostedFile.FileName)).ToLower();
            return timestamp + strEx;
        }

        /// <summary>
        /// 初始化返回DIV
        /// </summary>
        /// <param name="thumbnailFolderPath"></param>
        /// <param name="photoFilePath"></param>
        /// <returns></returns>
        public string InitDiv(string thumbnailFolderPath, string photoFilePath)
        {
            string photoUrl = thumbnailFolderPath + photoFilePath;
            StringBuilder text = new StringBuilder();
            text.Append("<div class='element'>");
            text.Append("    <div>");
            text.Append("        <img src='"+photoUrl+"' alt='' />");
            text.Append("    </div>");
            text.Append("    <p>");
            text.Append("        <input type='text' id='PPID_Explain_1' name='PPID_Explain_1' value='' />");
            text.Append("    </p>");
            text.Append("    <p class='buttonP'>");
            text.Append("        <img src='/Admin/Themes/Images/del.gif' alt='删除商品图片' class='delPhoto' /><a href='javascript:DelProductPhoto('1')'>删除</a>");
            text.Append("    </p>");
            text.Append("</div>");
            text.Append("");
            return text.ToString();
        }
    }
}